<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Jhub1_Common;

use Zend\Mvc\ModuleRouteListener;
use Zend\Session\SessionManager;
use Zend\Session\Container;
use Zend\Session\Validator\RemoteAddr;
use Zend\Session\Validator\HttpUserAgent;
use Zend\Mvc\MvcEvent;
use Application\Helper\AbsoluteUrl;
use Application\Helper\Environment;
use Application\Helper\LoginSignature;
use Application\Helper\UserName;
use Application\Helper\AuthenticationCheck;
use Zend\Console\Request as ConsoleRequest;

class Module
{
    protected $whitelist = array('home', 'authenticate', 'register', 'passwordreminder', 'activateaccount', 'activate', 'logout', 'dev_home');

    public function onBootstrap(MvcEvent $e)
    {
        $app         = $e->getApplication();
        /*        $config      = $app->getServiceManager()->get("configurator");
                $phpSettings = $config['phpSettings'];
                if($phpSettings) {
                    foreach($phpSettings as $key => $value) {
                        ini_set($key, $value);
                    }
                }*/
        $request = $app->getRequest();
        if ($request instanceof ConsoleRequest) {
            $eventManager = $e->getApplication()->getEventManager();
            $moduleRouteListener = new ModuleRouteListener();
            $moduleRouteListener->attach($eventManager);
        } else {
            $serviceManager = $app->getServiceManager();
            $app->getServiceManager()->get('translator');
            $eventManager = $app->getEventManager();
            $moduleRouteListener = new ModuleRouteListener();
            $moduleRouteListener->attach($eventManager);
            $list = $this->whitelist;

            $session_user = new Container('user');
            $auth = $session_user->offsetGet('userobject');

            $eventManager->attach(MvcEvent::EVENT_ROUTE, function ($e) use ($list, $auth) {
                $match = $e->getRouteMatch();
                $name = $match->getMatchedRouteName();

                if (in_array($name, $list)) {
                    return;
                }
                // User is authenticated

                /**
                 * @TODO improve authentication test
                 * autentication check must working with Admin, Developer, Rest
                 */
                if (null != $auth) {
                    /*                if (in_array($name, $list)) {
                                        $router = $e->getRouter();
                                        $url = $router->assemble(array('hub'), array(
                                            'action' => 'index'
                                        ));
                                        $response = $e->getResponse();
                                        $response->getHeaders()->addHeaderLine('Location', $url);
                                        $response->setStatusCode(302);
                                        return $response;
                                    }*/
                    return;
                }

                // Redirect to the user login page, as an example
                $router = $e->getRouter();
                $url = $router->assemble(array('home'), array(
                    'name' => 'home'
                ));
                $response = $e->getResponse();
                $response->getHeaders()->addHeaderLine('Location', $url);
                $response->setStatusCode(302);
                return $response;
            }, -100);
        }
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

}
